linux 监控 内存使用率

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 4522

2019/03/31 13:20


示例:点击 -> 性能监控

/proc/meminfo   used=total-(buffers+cached+free)

[root@wangzi go]# cat /proc/meminfo
MemTotal:        1016212 kB
MemFree:           77724 kB
MemAvailable:      51692 kB
Buffers:            8468 kB
Cached:            59584 kB

python监控代码:

#!/usr/bin/env python
# coding=utf-8
# author: brownwang
# mail: 277215243@qq.com
# datetime:2019/3/31 1:03 PM
# web: https://www.bthlt.com

def mem_use():
    free=0
    used=0
    total=0
    buffers=0
    cached=0
    with open('/proc/meminfo','r') as file:
        for line in file.readlines():
            if line.startswith('MemTotal:'):
                total=line.split()[1]
            if line.startswith('MemFree:'):
                free=line.split()[1]
            if line.startswith('Cached:'):
                cached=line.split()[1]
            if line.startswith('Buffers:'):
                buffers=line.split()[1]
    used=int(total)-int(free)-int(cached)-int(buffers)
    insert_sql="""insert into `monitor_mem_use` (`used`,`free`,`cached`,`buffers`,`flow_time`) values ({0},{1},{2},{3},'{4}')""".format(used,free,cached,buffers,now_zero)
    cursorUpdate(insert_sql,[])

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1